﻿using System;
using System.Data;
using System.Collections.Generic;
using Manager.Tools;
using Manager.Model.Admin;
namespace Manager.BLL.Admin
{
    /// <summary>
    /// M_OpusBll
    /// </summary>
    public partial class M_OpusBll
    {
        private readonly Manager.DAL.Admin.M_OpusDal dal = new Manager.DAL.Admin.M_OpusDal();
        public M_OpusBll()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int OpusID)
        {
            return dal.Exists(OpusID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Manager.Model.Admin.M_Opus model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Manager.Model.Admin.M_Opus model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int OpusID)
        {

            return dal.Delete(OpusID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string OpusIDlist)
        {
            return dal.DeleteList(OpusIDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Manager.Model.Admin.M_Opus GetModel(int OpusID)
        {

            return dal.GetModel(OpusID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Manager.Model.Admin.M_Opus GetModelByCache(int OpusID)
        {

            string CacheKey = "M_OpusModel-" + OpusID;
            object objModel = Manager.Tools.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(OpusID);
                    if (objModel != null)
                    {
                        int ModelCache = Manager.Tools.ConfigHelper.GetConfigInt("ModelCache");
                        Manager.Tools.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Manager.Model.Admin.M_Opus)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Manager.Model.Admin.M_Opus> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Manager.Model.Admin.M_Opus> DataTableToList(DataTable dt)
        {
            List<Manager.Model.Admin.M_Opus> modelList = new List<Manager.Model.Admin.M_Opus>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Manager.Model.Admin.M_Opus model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Manager.Model.Admin.M_Opus();
                    if (dt.Rows[n]["OpusID"] != null && dt.Rows[n]["OpusID"].ToString() != "")
                    {
                        model.OpusID = int.Parse(dt.Rows[n]["OpusID"].ToString());
                    }
                    if (dt.Rows[n]["OpusName"] != null && dt.Rows[n]["OpusName"].ToString() != "")
                    {
                        model.OpusName = dt.Rows[n]["OpusName"].ToString();
                    }
                    if (dt.Rows[n]["OpusDesc"] != null && dt.Rows[n]["OpusDesc"].ToString() != "")
                    {
                        model.OpusDesc = dt.Rows[n]["OpusDesc"].ToString();
                    }
                    if (dt.Rows[n]["OpusImage"] != null && dt.Rows[n]["OpusImage"].ToString() != "")
                    {
                        model.OpusImage = dt.Rows[n]["OpusImage"].ToString();
                    }
                    if (dt.Rows[n]["OpusStylistID"] != null && dt.Rows[n]["OpusStylistID"].ToString() != "")
                    {
                        model.OpusStylistID = int.Parse(dt.Rows[n]["OpusStylistID"].ToString());
                    }
                    if (dt.Rows[n]["IsValid"] != null && dt.Rows[n]["IsValid"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsValid"].ToString() == "1") || (dt.Rows[n]["IsValid"].ToString().ToLower() == "true"))
                        {
                            model.IsValid = true;
                        }
                        else
                        {
                            model.IsValid = false;
                        }
                    }
                    if (dt.Rows[n]["IsTop"] != null && dt.Rows[n]["IsTop"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsTop"].ToString() == "1") || (dt.Rows[n]["IsTop"].ToString().ToLower() == "true"))
                        {
                            model.IsTop = true;
                        }
                        else
                        {
                            model.IsTop = false;
                        }
                    }
                    if (dt.Rows[n]["OpusStylistDate"] != null && dt.Rows[n]["OpusStylistDate"].ToString() != "")
                    {
                        model.OpusStylistDate = DateTime.Parse(dt.Rows[n]["OpusStylistDate"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  Method
    }
}

